//
//  BHCompetitionSortView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/11.
//

import UIKit
protocol BHCompetitionSortViewDelegate {
    func didSortChoseIndex(model: BHHomeCityModel?,index: Int, view: BHCompetitionSortView)
    func didSortViewClose()
}
class BHCompetitionSortView: UIView {
    
    var delegate: BHCompetitionSortViewDelegate?
    

    lazy var tableView: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.register(UINib.init(nibName: "BHHomeChoseCityCell", bundle: nil), forCellReuseIdentifier: "BHHomeChoseCityCell")
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .color_f8f8f8
        view.bounces = false
        return view
    }()
    
    lazy var shadowLayerView: UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        return view
    }()
    
    var itemArray: [BHHomeCityModel] = [] {
        didSet {
            self.tableView.reloadData()
        }
    }
    
    var type: Int = 0
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isHidden = true
        self.addSubview(self.shadowLayerView)
        self.addSubview(self.tableView)
        
        self.shadowLayerView.mas_makeConstraints { (make) in
            make?.left.right()?.top()?.bottom()?.equalTo()(self)
        }
        self.tableView.mas_makeConstraints { (make) in
            make?.left.right()?.top()?.equalTo()(self)
            make?.height.mas_equalTo()(160)
        }
        
        self.shadowLayerView.addViewAction {[weak self] (_) in
            self?.showOrHide(show: false)
            self?.delegate?.didSortViewClose()
        }
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// animation
extension BHCompetitionSortView {
    func showOrHide(show: Bool) {
        self.isHidden = !show
    }
}

extension BHCompetitionSortView: UITableViewDataSource {
   
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.itemArray.count

    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BHHomeChoseCityCell", for: indexPath) as! BHHomeChoseCityCell
        let model = self.itemArray[indexPath.row]
        cell.text_label.text = model.name
        cell.canChose = false
        return cell
    }
}

extension BHCompetitionSortView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.itemArray[indexPath.row]
        self.delegate?.didSortChoseIndex(model: model, index: indexPath.row, view: self)
        
        self.showOrHide(show: false)
        self.delegate?.didSortViewClose()
    }
}
